package translate

import (
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"server/cfg"
	db3 "server/db"
	"server/game/bag"
	db4 "server/game/bag/db"
	"server/game/item"
	"server/game/player"
	"server/game/player/db"
	db2 "server/game/translate/db"
)

const BreakfastTableName = "t_breakfast"

// Breakfast 早餐结构体
type Breakfast struct {
	Name         string            `bson:"name" json:"name"`
	Status       string            `bson:"status" json:"status"`
	Decorative   db.DecorativeItem `bson:"decorative" json:"decorative"`
	ProductionID int               `bson:"productionID" json:"production_id"`
	Num          int               `bson:"num" json:"num"`
}
type PlayerBreakfast struct {
	UID             string         `bson:"uid" json:"uid"`
	Cid             int            `bson:"cid" json:"cid"`
	IdentityLevel   int            `json:"identityLevel" bson:"identityLevel"`
	BreakfastStall  BreakfastStall `json:"breakfast_stall" bson:"breakfast_stall"`
	Employees       []string       `bson:"employees" json:"employees"`
	ProductionTools []db2.Tool     `bson:"production_tools" json:"production_tools"` //扩展栏位
	Breakfasts      []Breakfast    `bson:"breakfasts" json:"breakfasts"`             //生产队列
	IsUnLock        bool           `bson:"is_un_lock" json:"is_un_lock"`
}
type CardToole struct {
	CardId   string `bson:"card_id" json:"card_id"`
	StarTime int64  `bson:"star_time" json:"star_time"`
	JobType  int    `bson:"job_type" json:"job_type"`
}

// BreakfastStall 早餐摊结构体
type BreakfastStall struct {
	IncomeSpeed     int         `bson:"income_speed" json:"income_speed"`
	IncomeBoost     float64     `bson:"income_boost" json:"income_boost"`
	Breakfasts      []Breakfast `bson:"breakfasts" json:"breakfasts"`
	ProductionTools []db2.Tool  `bson:"production_tools" json:"production_tools"`
}

// 初始化玩家
func NewPlayer(uid string, cid int) *PlayerBreakfast {
	return &PlayerBreakfast{
		UID:           uid,
		Cid:           cid,
		IdentityLevel: 1,
		BreakfastStall: BreakfastStall{
			IncomeSpeed: 10,
			IncomeBoost: 1.0,
		},
		ProductionTools: []db2.Tool{
			{Name: "Tool1", Level: 1, Decorative: db.DecorativeItem{}, UpgradeCost: 100},
		},
		Breakfasts: []Breakfast{
			{Name: "Breakfast1", Status: "Initial", Decorative: db.DecorativeItem{}, ProductionID: 1},
		},
	}
}
func CreateUserBreakfast(uid string, Cid int) (*PlayerBreakfast, error) {
	var info *PlayerBreakfast
	err := db3.Db.FindOne(BreakfastTableName, bson.M{"uid": uid}, info)
	if err == nil {
		return nil, fmt.Errorf("%s已经有数据，不能初始化了", uid)
	}
	info = NewPlayer(uid, Cid)
	err = db3.Db.Insert(BreakfastTableName, info)
	if err != nil {
		return nil, err
	}
	return info, nil
}
func GetBreakfast(uid string, cid int) (*PlayerBreakfast, error) {
	var info = &PlayerBreakfast{}
	err := db3.Db.FindOne(BreakfastTableName, bson.M{"uid": uid, "cid": cid}, info)
	if err != nil {
		return nil, err
	}
	return info, nil
}
func GetBreakfastList(uid string) ([]PlayerBreakfast, error) {
	var info []PlayerBreakfast
	err := db3.Db.Find(BreakfastTableName, bson.M{"uid": uid}, &info)
	if err != nil {
		return nil, err
	}
	return info, nil
}
func (p *PlayerBreakfast) UnlockBreakfastTools(uid string, cid, tid int) (PlayerBreakfast, error) {
	var info PlayerBreakfast
	err := db3.Db.FindOne(BreakfastTableName, bson.M{"uid": uid, "cid": cid}, &info)
	if err != nil {
		return info, err
	}
	info.ProductionTools = append(info.ProductionTools,
		db2.Tool{Name: "Tool1", Level: 1, Decorative: db.DecorativeItem{}, UpgradeCost: 100})
	return info, nil

}
func (p *PlayerBreakfast) UnlockBreakfastBreakfast(uid string, cid, Bid int) (PlayerBreakfast, error) {
	var info PlayerBreakfast
	err := db3.Db.FindOne(BreakfastTableName, bson.M{"uid": uid, "cid": cid}, &info)
	if err != nil {
		return info, err
	}
	info.Breakfasts = append(info.Breakfasts,
		Breakfast{Name: "Breakfast1", Status: "Initial", Decorative: db.DecorativeItem{}, ProductionID: 1})
	return info, nil
}

// 制作早餐
func (p *PlayerBreakfast) ProduceBreakfast(breakfastIndex int, num int) error {
	if breakfastIndex < 0 || breakfastIndex >= len(p.Breakfasts) {
		return fmt.Errorf("Invalid breakfast index")
	}
	cfg := cfg.ConfigData.TbBuildBusiness.Get(int32(p.Cid))
	p.Breakfasts[breakfastIndex].Status = "Processing"
	p.Breakfasts[breakfastIndex].Name = cfg.Name
	p.Breakfasts[breakfastIndex].Num = num
	err := db3.Db.Update(BreakfastTableName,
		bson.M{"uid": p.UID, "cid": p.Cid},
		map[string]interface{}{
			"$set": map[string]interface{}{"breakfasts": p.Breakfasts},
		})
	return err
}

// 完成早餐制作
func (p *PlayerBreakfast) FinishBreakfast(breakfastIndex int, num int) ([]item.Item, int, error) {
	if breakfastIndex < 0 || breakfastIndex >= len(p.Breakfasts) {
		return nil, 0, fmt.Errorf("Invalid breakfast index")
	}
	p.Breakfasts[breakfastIndex].Num = p.Breakfasts[breakfastIndex].Num - num
	if p.Breakfasts[breakfastIndex].Num <= 0 {
		p.Breakfasts[breakfastIndex].Status = "Finished"
	}

	db3.Db.Update(BreakfastTableName,
		bson.M{"uid": p.UID, "cid": p.Cid},
		map[string]interface{}{
			"$set": map[string]interface{}{"breakfasts": p.Breakfasts},
		})
	cfg := cfg.ConfigData.TbBuildBusiness.Get(int32(p.Cid))
	zhuanQian := float32(cfg.EarnPlus*int32(num)) * (cfg.EarnMulti*float32(num) + 1)
	zhuanQian = float32(int(zhuanQian) * num)
	item, err := bag.EnAddItem(p.UID, db4.Chaopiao, int(zhuanQian))
	if err != nil {
		return nil, 0, err
	}
	//p.Coins += 10 // 每次完成获得 10 个钱币
	return item, int(zhuanQian), nil
}
func (p *PlayerBreakfast) UpgradeTool(toolIndex int) error {
	if toolIndex < 0 || toolIndex >= len(p.ProductionTools) {
		return fmt.Errorf("Invalid tool index")
	}
	tool := &p.ProductionTools[toolIndex]
	//if p.Coins < tool.UpgradeCost {
	//	return fmt.Errorf("Not enough coins to upgrade tool")
	//}
	//p.Coins -= tool.UpgradeCost
	tool.Level++
	tool.Decorative.Obtained = true
	tool.Decorative.ObtainCondition = "Upgraded"
	tool.Decorative.Validity = "Permanent"
	tool.Decorative.DisplayType = "Tool"
	return nil
}

// NPC 互动
func (p *PlayerBreakfast) InteractWithNPC(breakfastIndex int) error {
	if p.Breakfasts[breakfastIndex].Status != "Finished" {
		return fmt.Errorf("还没做好")
	}
	p.Breakfasts[breakfastIndex].Status = "Initial"
	player.AddUserCoins(p.UID, 10)
	return nil
}

func (p *PlayerBreakfast) ZhaoGong(cardId string) {

}
